package com.example.demo.controller;

import com.example.demo.base.*;
import com.example.demo.entity.Admin;
import com.example.demo.entity.User;
import com.example.demo.service.AdminService;
import com.example.demo.service.UserService;
import com.example.demo.vo.AdminVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@Validated
@RequestMapping({"/manage/"})
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private AdminService adminService;

    @RequestMapping(value = "/admin/adminList")
    public BaseResponse getUserList(
            @RequestParam(required = false, value = "platForm") String platForm,
            @RequestParam(required = false, value = "userName") String userName,
            @RequestParam(required = false, value = "deleted") Integer deleted,
            @RequestParam(required = false, value = "page") Integer pageNo,
            @RequestParam(required = false, value = "rows") Integer pageSize,
            HttpServletRequest request) {
        QueryBuilder queryBuilder = new QueryBuilder();
        queryBuilder.addFilter(Filter.eq("state",0));
        if(StringUtils.isNotBlank(platForm)){
            queryBuilder.addFilter(Filter.eq("plat_form",platForm));
        }
        if(StringUtils.isNotBlank(userName)){
            queryBuilder.addFilter(Filter.eq("user_name",userName));
        }
        if(deleted==0){
            queryBuilder.setDeleted(true);
        }
        Pageable<User> pageable = userService.selectEntitiesByPage(queryBuilder,pageNo,pageSize);

        BaseResponse response = new BaseResponse();
        if(pageable!=null && !CollectionUtils.isEmpty(pageable.getRecords())){
            List<AdminVO> list = new ArrayList<>();
            for(User user:pageable.getRecords()){
                queryBuilder.clear();
                queryBuilder.addFilter(Filter.eq("user_id",user.getId()));
                Admin admin = adminService.selectEntity(queryBuilder);
                AdminVO adminVO = new AdminVO(user,admin);
                list.add(adminVO);
            }
            response.setRows(list);
            response.setTotal(new Long(pageable.getTotal()).intValue());
        }
        return response;
    }

    @RequestMapping(value = "/admin/update")
    public BaseResponse updateUser(
            @RequestParam(required = false, value = "userId") Long id,
            @RequestParam(required = false, value = "password") String password,
            @RequestParam(required = false, value = "sex") String sex,
            @RequestParam(required = false, value = "address") String address,
            @RequestParam(required = false, value = "phone") String phone,
            @RequestParam(required = false, value = "sign") Integer sign,
            HttpServletRequest request) {
        User user = new User();
        Admin admin = new Admin();
        if(sign==0){//删除
            user.setDeleted(1);
        }else if(sign==1){
            user.setPassword(com.example.demo.util.StringUtils.MD5Encode(password));
            admin.setSex(sex);
            admin.setAddress(address);
            admin.setPhone(phone);
        }else{
            user.setDeleted(0);
        }
        UpdateBuilder updateBuilder = new UpdateBuilder(user);
        updateBuilder.addFilter(Filter.eq("id",id));
        userService.updateEntity(updateBuilder);

        QueryBuilder queryBuilder = new QueryBuilder();
        queryBuilder.addFilter(Filter.eq("user_id",id));
        Admin admin1 = adminService.selectEntity(queryBuilder);
        if(admin1 == null){
            admin.setUserId(id);
            adminService.insertEntity0(admin);
        }else{
            updateBuilder.clear();
            updateBuilder.setEntity(admin);
            updateBuilder.addFilter(Filter.eq("user_id",id));
            adminService.updateEntity(updateBuilder);
        }
        return BaseResponse.success();
    }
}
